package com.atguigu.cloud.controller;


import com.atguigu.cloud.entities.Pay;

import com.aiguigu.cloud.entities.PayDTO;
import com.aiguigu.cloud.resp.ResultData;

import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j //日志
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "新增支付流水，json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){

        System.out.println(pay.toString());
        int result = payService.add(pay);

        return ResultData.success("成功插入记录，返回值" + result);
    }

    @DeleteMapping(value = "/pay/delete/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id){

        Integer result = payService.delete(id);
        return ResultData.success(result);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){

        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int result = payService.update(pay);

        return ResultData.success("修改成功，返回值" + result);
    }

    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按ID查找流水", description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){

        if (id < 0){
            throw new RuntimeException("id不能为负数");
        }
        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @GetMapping(value = "pay/get/all")
    @Operation(summary = "查询所有支付流水", description = "查询所有支付流水方法")
    public ResultData<List<Pay>> getAll(){

        List<Pay> payList = payService.getAll();
        return ResultData.success(payList);
    }

    @Value(("${server.port}"))
    private String port;
    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String info){
        return "ConsulInfo: " + info + "\t" + "port: " + port;
    }

}
